package com.binvshe.binvshe.common.http;

import android.os.Bundle;

import com.pachong.android.baseuicomponent.activity.SwipeListActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/12/30
 * lastid 解析器 的SwipeListActivity 基类
 */

public abstract class BaseParserActivity<BeanType extends Object> extends SwipeListActivity
        implements ReflexInterface<BeanType> {
    protected final static int PAGE_SIZE = 10;
    protected int lastListSize = 0;
    int mlastid = 0;

    public MyResponseParser<BeanType> parser = new MyResponseParser<BeanType>(getReflexClass()) {

        @Override
        public boolean hasMore() {
            if (lastListSize >= PAGE_SIZE) {
                return true;
            } else {
                return false;
            }
        }
    };


    ListDataRequestListener mRequestListener = new ListDataRequestListener(this, parser) {
        @Override
        public void onSuccess(Object listData) {
            super.onSuccess(listData);
            if (getData().size() > 0) {
                mlastid = getLastID((BeanType) getData().get(getData().size() - 1));
            }
            List<BeanType> list = (List<BeanType>) listData;
            if (list != null) {
                lastListSize = list.size();
            }
        }
    };

    public abstract int getLastID(BeanType data);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startLoading();
    }

    @Override
    public abstract RecyclerViewBaseAdapter createAdapter();

    @Override
    public abstract void onStartLoading();


    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        mlastid = 0;
        getData().clear();
        startLoading();
    }

    @Override
    public Class<BeanType> getReflexClass() {
        Class<BeanType> entityClass = (Class<BeanType>) ((ParameterizedType)
                this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return entityClass;
    }
}
